package com.application.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatTextView;

import java.util.ArrayList;
import java.util.List;

public class FiveView extends AppCompatTextView {

    private int Width, Hight;
    private Paint mPaint;
    private int mBroad;
    private float mLineWidth;
    private static final int MAX_LINE = 10;
    String string;
    Paint mTextPatin;
    private int backGround;
    private int lin;

    public FiveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);
        //获取自定义的属性
        final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FiveView);
        //获取设置的文本值
        string = array.getString(R.styleable.FiveView_top_text);
        //获取背景色
        backGround = array.getColor(R.styleable.FiveView_bgColor, 0);
        //获取线条宽度
        lin = array.getDimensionPixelSize(R.styleable.FiveView_line_width, 0);
        mPaint.setStrokeWidth(lin);
        mTextPatin = new Paint();
        //设置字体颜色
        mTextPatin.setColor(Color.BLACK);
        //设置字体大小
        mTextPatin.setTextSize(getTextSize());

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Width = MeasureSpec.getSize(widthMeasureSpec);
        Hight = MeasureSpec.getSize(heightMeasureSpec);
        int WidthMode = MeasureSpec.getMode(widthMeasureSpec);

        // Width = Math.min(Width, Hight);
        int measuredWidth = MeasureSpec.makeMeasureSpec(Width, WidthMode);
        setMeasuredDimension(measuredWidth, Hight);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBroad = Width;
        mLineWidth = mBroad * 1.0f / MAX_LINE;

    }


    @Override
    protected void onDraw(Canvas canvas) {
        //定义画笔
        Paint paint = new Paint();
        //设置颜色
        paint.setColor(backGround);
        //设置画笔类型
        paint.setStyle(Paint.Style.FILL);
        //画背景色
        canvas.drawRect(0, 150, Width, Hight, paint);

        DrawBroad(canvas);
    }


    private void DrawBroad(Canvas canvas) {

        for (int i = 0; i < MAX_LINE; i++) {
            //画横线
            float startX = 0;
            float stopX = mBroad;
            float startY = (Hight / MAX_LINE) * i + 150;
            float stopY = (Hight / MAX_LINE) * i + 150;
            canvas.drawLine(startX, startY, stopX, stopY, mPaint);
            //画竖线
            float startX1 = 150;
            float stopX1 = Hight;
            float startY1 = (Width / MAX_LINE) * i;
            float stopY1 = (Width / MAX_LINE) * i;
            canvas.drawLine(startY1, startX1, stopY1, stopX1, mPaint);
        }
        //画字体
        canvas.drawText(string, Width / 2-mTextPatin.measureText(string)/2, 75, mTextPatin);

    }


}